package com.ny.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String formEmail;
    public void sendEmail(String toEmail,String code) {
        // 创建邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件内容
        message.setFrom(formEmail); // 发件人
        message.setTo(toEmail);     // 收件人
        message.setSubject("验证码"); // 邮件主题
        message.setText("验证码为："+code);      // 邮件内容
        mailSender.send(message);      // 发送邮件
    }

    //随机生成验证码
    public String getCode() {
        int code = (int)((Math.random()*9000)+1000);
        return String.valueOf(code);
    }
}
